package com.c.ctgproject.fragment.review.part

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.review.ReviewPartsCommonAdapter
import com.c.ctgproject.base.BaseFragment
import com.c.ctgproject.entity.review.PartReviewEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.ui.review.part.PartOrderDetailActivity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.fragment_refuse_parts.mRecyclerView
import kotlinx.android.synthetic.main.fragment_refuse_parts.mRefresh
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2020/9/9
 *描述:配件审核  已拒绝
 */
class RefusePartsFragment : BaseFragment(), ReviewPartsCommonAdapter.PartsLiteItemClick {

    private val mViewModel: RefusePartsViewModel by lazy { ViewModelProvider(requireActivity())[RefusePartsViewModel::class.java] }

    private var page = 1
    private var size = 10
    private var mList: ArrayList<PartReviewEntity.DataBean.ListBean> = ArrayList()
    private val mAdapter: ReviewPartsCommonAdapter by lazy {
        ReviewPartsCommonAdapter(requireContext(), mList,this)
    }

    override fun event() {
        mRecyclerView.adapter=mAdapter
        mRecyclerView.layoutManager= LinearLayoutManager(requireContext())


        mViewModel.result.observe(this, Observer {
            MyLog.d("配件拒绝列表回调:$it")
            val data= initData(it)
            if (data.status == 0) {
                mRefresh.finishRefresh()
                mRefresh.finishLoadMore()
                val entity = getGson().fromJson(it, PartReviewEntity::class.java).data
                entity?.list?.let {list->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                requireContext().showToast(data.msg)
            }
        })
        mViewModel.error.observe(this, Observer {
            mRefresh.finishRefresh()
            mRefresh.finishLoadMore()
            MyLog.d("错误回调:$it")
            activity?.let { it1 -> CustomToast.showToast(it1, it) }
        })
    }

    override fun click() {
        //下拉刷新监听
        mRefresh.setOnRefreshListener {
           initData()
        }
        //上拉加载
        mRefresh.setOnLoadMoreListener {
            page++
            mViewModel.getList(page, size)
        }
    }
    private fun initData(){
        page = 1
        mList.clear()
        mViewModel.getList(page, size)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }
    override fun getLayout() = R.layout.fragment_refuse_parts
    override fun itemClick(data: PartReviewEntity.DataBean.ListBean) {
        requireContext().startActivity<PartOrderDetailActivity>(
            MyConstants.ENTITY to data,
            MyConstants.AGREE_TYPE to MyConstants.REFUSE
        )
    }

}